<template>
  <div class="sub-category" v-if="subCategoryData.parentId">
    <div class="container">
      <!-- 面包屑区域 -->
      <XtxBread>
        <XtxBreadItem to="/">首页</XtxBreadItem>
        <XtxBreadItem :to="`/category/${subCategoryData.parentId}`">
          {{ subCategoryData.parentName }}
        </XtxBreadItem>
        <XtxBreadItem>{{ subCategoryData.name }}</XtxBreadItem>
      </XtxBread>
      <div class="sub-filter">
        <!-- 品牌 -->
        <div class="item">
          <div class="head">品牌：</div>
          <div class="body">
            <a
              href="javascript:;"
              v-for="brand in subCategoryData.brands"
              :key="brand.id"
              >{{ brand.name }}</a
            >
          </div>
        </div>
        <!-- 规格 -->
        <div
          class="item"
          v-for="item in subCategoryData.saleProperties"
          :key="item.id"
        >
          <div class="head">{{ item.name }}：</div>
          <div class="body">
            <a
              href="javascript:;"
              v-for="i in item.properties"
              :key="i.id"
              @click="toggleSelected(i,item)"
              :class="{ active: i.selected === true }"
              >{{ i.name }}</a
            >
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { findSubCategoryFilter } from '@/api/category'
import { ref, unref, watch } from 'vue'
import { useRoute } from 'vue-router'
export default {
  name: 'SubCategory',
  setup () {
    const subCategoryData = ref({})
    const route = useRoute()
    async function loadSubData () {
      const res = await findSubCategoryFilter(route.params.id)
      subCategoryData.value = res.result
      console.log(res)
      initData(subCategoryData)
    }
    // 处理额外的全部分类
    function initData (data) {
      // 兼容ref或者非ref
      data = unref(data)
      data.brands.unshift({
        name: '全部',
        id: null
      })
      data.saleProperties.forEach((item) => {
        item.properties.unshift({
          name: '全部',
          id: null
        })
        // 给每一个规格添加一个专门用来控制是否激活的字段 selected true 激活 false 不激活
        item.properties.forEach(item => {
          item.selected = false
        })
      })
    }

    function toggleSelected (i, item) {
      console.log(item)
      console.log(i)
      item.properties.forEach(item => {
        item.selected = false
      })
      i.selected = true
    }
    watch(() => {
      loadSubData()
    })
    return {
      subCategoryData,
      toggleSelected
    }
  }
}
</script>
<style scoped lang='less'>
.goods-list {
  background: #fff;
  padding: 0 25px;
  margin-top: 25px;
}
// 筛选区
.sub-filter {
  background: #fff;
  padding: 25px;
  .item {
    display: flex;
    padding: 10px 0;
    .head {
      width: 80px;
      color: #999;
    }
    .body {
      flex: 1;
      a {
        margin-right: 36px;
        transition: all 0.3s;
        &.active,
        &:hover {
          color: @xtxColor;
        }
      }
    }
  }
}
</style>
